
$(function () {
	
	//操作时间
	$('#cTime_start').daterangepicker({
		"singleDatePicker": true,
		"timePicker": true,
		"timePicker24Hour": true,
		"timePickerSeconds": true,
		"parentEl": "200",
		locale: {
		  format:'YYYY-MM-DD HH:mm:ss',
	      applyLabel: '确认',
	      cancelLabel: '取消',
	      weekLabel: 'W',
	      customRangeLabel: 'Custom Range',
	      daysOfWeek:["日","一","二","三","四","五","六"],
	      monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
		}
	}, function(start, end, label) {
		$('#cTime_start').val(start.format('YYYY-MM-DD HH:mm:ss'));
	});
	$("#cTime_start").val("");//清空控件默认时间
	
	$('#cTime_end').daterangepicker({
		"singleDatePicker": true,
		"timePicker": true,
		"timePicker24Hour": true,
		"timePickerSeconds": true,
		"parentEl": "200",
		locale: {
		  format:'YYYY-MM-DD HH:mm:ss',
	      applyLabel: '确认',
	      cancelLabel: '取消',
	      weekLabel: 'W',
	      customRangeLabel: 'Custom Range',
	      daysOfWeek:["日","一","二","三","四","五","六"],
	      monthNames: ["一月","二月","三月","四月","五月","六月","七月","八月","九月","十月","十一月","十二月"]
		}
	}, function(start, end, label) {
		$('#cTime_end').val(start.format('YYYY-MM-DD HH:mm:ss'));
	});
	$('#cTime_end').val("");//清空控件默认时间
	
	//日期时间查询条件不为空的情况下，给控件赋值
	if($("#cTimeS_hidden").val() != null || $("#cTimeS_hidden").val() != ""){
		$("#cTime_start").val($("#cTimeS_hidden").val());
	}
	if($("#cTimeE_hidden").val() != null || $("#cTimeE_hidden").val() != ""){
		$("#cTime_end").val($("#cTimeE_hidden").val());
	}
	
	
	//加载异步数据 (获取节点名称列表)
    $.get("/lgsNode/getNodeList?state=1", function (re) {
        var nodeList = re.data;
        var form = window.queryForm;
        var nodeOptions = "";
        for (var i in nodeList) {
            var node = nodeList[i];
            nodeOptions += "<option value='" + node.id + "'>" + node.name + "</option>";
        }
        $(window.queryForm.node).append(nodeOptions);
        
        if($("#node_hidden").val()){
        	$("[name='node']").find("option[value='"+$("#node_hidden").val()+"']").attr("selected",true);
        }
    });

  //加载异步数据（获取扫描地点）
    $.get("/lgsBills/allDepotList?state=1", function (re) {
        var depotList = re.data;
        var depotOptions = "";
        for (var i in depotList) {
            var depot = depotList[i];
            depotOptions += "<option value='" + depot.id + "'>" + depot.name + "</option>";
        }
        $("#depotScan").append(depotOptions);
		var scanDepot=$("#scan_hidden").val();
        if(scanDepot){
        	$("#depotScan").find("option[value='"+scanDepot+"']").attr("selected",true);
        }

    });

    //加载异步数据（收/发货对象）
    $.get("/lgsBills/getCustList?state=1", function (re) {
        var custList = re.data;
        var custOptions = "";
        for (var i in custList) {
            var cust = custList[i];
            custOptions += "<option value='" + cust.id + "'>" + cust.custName + "</option>";
        }
        $("#toDepot").append(custOptions);
        $("#frmDepot").append(custOptions);
        var toDepot=$("#to_hidden").val(),fromDeot=$("#from_hidden").val();

        if(toDepot){
            $("#toDepot").find("option[value='"+toDepot+"']").attr("selected",true);
        }
        if(fromDeot){
            $("#frmDepot").find("option[value='"+fromDeot+"']").attr("selected",true);
        }
    });
	

    //点击“创建单据”按钮，
    $("[name='btn_new_bill']").click(function(){
    	window.location.href="/lgsBills/add";
    });
    
    
	//作废单据
	$("[name='btn_invalid_bill']").click(function(){
		 var id = $(this).data("bill-id");
		
	     bootbox.bconfirm("确定要做作废单据吗？", function (re) {
	         if (re) {
	        	 $.post("/lgsBills/invalid?id="+id, function (re) {
	                 if (re.data) {
	                     // tipper.success("删除成功，请刷新页面");
	                	 window.location.href=window.location.href; 
	                	 window.location.reload; 
	                 } else {
	                     //tipper.danger("操作失败，请重试");
	                 }
	             });
	        	 
	         }
	     });
	});


//点击单据列表中"查看"按钮
$("[name='btn_bill_detail']").click(function(){
	var billId = $(this).data("bill-id");
	window.location.href="/lgsBills/getBillDetail?billId="+billId;
});
//点击单据列表中"明细"按钮
$("[name='btn_bill_item']").click(function(){
	var billId = $(this).data("bill-id");
	window.location.href="/lgsBills/item?billId="+billId;
});





//查询条件重置
$("#search_reset").click(function(){
	$("input").val("");
    $("select").val("");
});


//数据导出
$("[name='btn_export']").click(function () {
	// String productName,
    // String billsNo, Integer billsType, Integer node, Integer nubsS, Integer nubsE,
     //    Integer from, Integer to, Integer depotScan, String cTimeS, String cTimeE, Integer sta
    var billsNo = $("[name='billsNo']").val(),node=$("[name='node']").val(),sta=$("[name='sta']").val();
    var productName = $("[name='productName']").val(),billsType = $("[name='billsType']").val();
    var nubsS=$("#nubsS").val(), nubsE=$("#nubsE").val(),depotScan=$("#depotScan").val();
	var to=$("#toDepot").val(),from=$("#frmDepot").val(),cTimeS=$("#cTime_start").val(),cTimeE=$("#cTime_end").val();
	var scanNubsS=$("#scanNubsS").val(),scanNubsE=$("#scanNubsE").val();

    //有数据时才能导出
    if($("#resultTotal_hidden").val() != 0){
    	bootbox.bconfirm("确定是否导出？", function (re) {
            if (re) {
            	 var url="/lgsBills/export?billsNo="+billsNo+"&node="+node+"&sta="+sta+"&productName="+productName+"&billsType="+billsType
					 +"&nubsS="+nubsS +"&nubsE="+nubsE+"&scanNubsS="+scanNubsS+"&scanNubsE=" +scanNubsE+"&depotScan="+depotScan+"&to="+to+"&from="+from+"&cTimeS="+cTimeS
                     +"&cTimeE="+cTimeE ;
                 window.open(url);
            }
        });
    }else{
    	bootbox.balert("没有数据，无法导出!");
    }
});

});
